package com.hadoop.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MyJob {

    public static void main(String[] args) {
        Configuration conf = new Configuration(true);
        //Job job = Job.getInstance(conf);
        Job job = null;
        try {
            job = Job.getInstance(conf, "word count");


            // Create a new Job
            //Job job = Job.getInstance();
            job.setJarByClass(MyJob.class);

            // Specify various job-specific parameters
            job.setJobName("myjob");

            Path input = new Path("/user/root/wordcount/input");
            FileInputFormat.addInputPath(job, input);

            Path output = new Path("/user/root/wordcount/output");
            if (output.getFileSystem(conf).exists(output)) {
                output.getFileSystem(conf).delete(output, true);
            }
            FileOutputFormat.setOutputPath(job, output);
            job.setMapperClass(MyMapper.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            job.setReducerClass(MyReducer.class);
            // Submit the job, then poll for progress until the job is
            job.waitForCompletion(true);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
